﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Models
{
    public class ShoppingCartsViewModel
    {
        IDinnerTableService dinnerTableService;
        public ShoppingCartsViewModel()
        {
            dinnerTableService = new DinnerTableService();
        }

        public List<ShoppingCartViewModel> shoppingCarts;
        public List<ShoppingCartViewModel> ShoppingCarts
        {
            get
            {
                if (shoppingCarts == null)
                    shoppingCarts = new List<ShoppingCartViewModel>();
                return shoppingCarts;
            }
        }

        public string Name
        {
            get
            {
                if (ShoppingCarts.Count > 0)
                    return shoppingCarts[0].Restaurant.Name;
                return "";
            }
        }

        private List<DinnerTableViewModel> dinnerTables;
        public List<DinnerTableViewModel> DinnerTables
        {
            get
            {
                if (dinnerTables == null)
                {
                    
                    var dts = dinnerTableService.FindList(1000, "", ShoppingCarts[0].Restaurant_Id, 0);

                    dinnerTables = new List<DinnerTableViewModel>();
                    foreach(var dt in dts)
                    {
                        dinnerTables.Add(DinnerTableViewModel.FromDinnerTable(dt));
                    }
                }

                return dinnerTables;
            }
        }

        private List<SelectListItem> tableItems;
        public List<SelectListItem> TableItems
        {
            get
            {
                if (tableItems == null)
                {
                    tableItems = new List<SelectListItem>();
                    foreach (var dt in DinnerTables)
                    {
                        tableItems.Add(new SelectListItem()
                        {
                            Text =  dt.Name + (dt.IsReserved?"[已预订]":""),
                            Value = dt.Id + "",
                            Disabled = dt.IsReserved
                        });
                    }
                }
                return tableItems;
            }
        }

        public int Total
        {
            get
            {
                return ShoppingCarts.Sum(c=>c.Total);
            }
        }

        public decimal Price
        {
            get
            {
                return shoppingCarts.Sum(c => c.Total * c.Food.Price);
            }
        }
    }
}